<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="can()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>{{ form.title }}
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择上级菜单"
            @change="fn()"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in hh"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option> </el-select
          >{{ form.pid }}
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio disabled :label="1" v-model="form.type">目录</el-radio>
          <el-radio disabled :label="2" v-model="form.type">菜单</el-radio>
          {{ form.type }}
        </el-form-item>
        <el-form-item v-if="form.type == 1" label="菜单图标">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="(item, index) in iconArr"
              :key="index"
              :value="item"
            >
              <i :class="item"></i>
            </el-option> </el-select
          >{{ form.icon }}
        </el-form-item>
        <el-form-item v-if="form.type == 2" label="菜单地址">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="(item, index) in secondRouter"
              :key="index"
              :value="'/' + item.path"
              :label="item.meta.title"
            >
            </el-option> </el-select
          >{{ form.url }}
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch> </el-form-item
        >{{ form.status }}
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="can()">取 消</el-button>
        <el-button v-if="obj.isAdd" @click="add()" type="primary"
          >添加</el-button
        >
        <el-button @click="bj()" v-else type="primary">编辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { secondRouter } from "../../../router/index.js";
import { menuAdd, menuinfo, menuedit } from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      ss: false,
      form: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      iconArr: [
        "el-icon-eleme",
        "el-icon-phone",
        "el-icon-delete",
        "el-icon-goods",
        "el-icon-info",
      ],
      secondRouter: secondRouter,
    };
  },

  mounted() {},

  methods: {
    ...mapActions({
      sss: "menuList/asyncChangeList",
    }),
    fn() {
      if (this.form.pid == 0) {
        this.form.type = 1;
        this.form.url = "";
      } else {
        this.form.type = 2;
        this.form.icon = "";
      }
    },
    //清空
    empty() {
      this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    //取消
    can() {
      this.obj.isTrue = false;
      this.empty();
    },
    //添加
    add() {
      menuAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.sss();
          this.can();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    //获取详情信息
    huo(id) {
      menuinfo({ id: id }).then((res) => {
        this.form = res.data.list;
        this.form.id = id;
      });
    },
    bj() {
      menuedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.can();
          this.sss();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      hh: "menuList/list",
    }),
  },
};
</script>

<style lang="scss" scoped>
</style>